# 利用tailhead软件包跟踪文件变化
import tailhead
# 字符串解析
import parse
# 时间包
import datetime
import os
import time

# 调用客户端
from thingsboard_gateway.tb_client.tb_device_mqtt import TBDeviceMqttClient

# 用于自动生成命令行
import fire

# 输出log，便于调试
import logging
logging.basicConfig(level = logging.INFO)
file_base_name = os.path.basename(__file__)
logger = logging.getLogger(file_base_name)


# 初始化
# 列名
col_names = 'YEAR	DAY	SecAftMid	TofType	IonType	MS_AB	PToF_AB	Flow	Sulf	Nitr	Org_Low	Cl	Imz_43	Imz_44	Imz_57	usmz_01	Iusmz_01	usmz_02	Iusmz_02	usmz_03	Iusmz_03	usmz_04	Iusmz_04	usmz_05	Iusmz_05	usmz_06	Iusmz_06	usmz_07	Iusmz_07	usmz_08	Iusmz_08	usmz_09	Iusmz_09	usmz_10	Iusmz_10	aiV0	aiV1	aiV2	aiV3	aiV4	aiV5	aiV6	aiV7	'.split('\t')[:-1]
# 格式
write_formats = ['04d','03d','8.2f','1d','1d',	'8.2f','08d']+['5.3f']*8+['04d','5.3f']*10 +['5.3f']*8
read_formats = [format[-1] for format in write_formats]
# 分隔符
sep = '\t'
# 默认值，为保持完整性
defaults_str = '2021	019	31800.59	1	0	33201.70	00000000	1.318	1.529	3.158	3.206	0.408	0.184	0.487	0.059	0030	1.198	0041	0.756	0046	0.890	0048	0.209	0055	0.081	0064	0.275	0069	0.043	0077	0.020	0091	0.027	0202	0.011	0.772	4.411	4.021	1.027	0.967	0.966	0.927	0.866	'

# 第一行
header = sep.join(col_names)+sep
logger.info(header)

# 生成格式化字符串
line_write_formatter=sep.join([f'{{{col_name}:{format}}}' for col_name,format in zip(col_names,write_formats)])+sep
logger.info(line_write_formatter)

line_read_formatter=sep.join([f'{{{col_name}:{format}}}' for col_name,format in zip(col_names,read_formats)])+sep
logger.info(line_read_formatter)

# 简单测试
parse_result = parse.parse(line_read_formatter,defaults_str)
logger.info(parse_result)

# 默认值
data_dict_defaults = parse_result.named 

# example data
# raw_data_example_filename = r"raw_data\课题3\AMS\AMS_TimeTrace_033270.txt"

def main(host='test2.bj-jiashi.com',
        port=10006,
        token='AMS',
        delay=0.5, 
        target_log_file='sub3_AMS_simulator.py.test.log',
        log_level = 'INFO'):
    # 初始化
    logging.basicConfig(level = getattr(logging,log_level.upper(),logging.INFO)) # 默认info
    file_base_name = os.path.basename(__file__)
    logger = logging.getLogger(file_base_name)

    # 列名
    col_names = 'YEAR	DAY	SecAftMid	TofType	IonType	MS_AB	PToF_AB	Flow	Sulf	Nitr	Org_Low	Cl	Imz_43	Imz_44	Imz_57	usmz_01	Iusmz_01	usmz_02	Iusmz_02	usmz_03	Iusmz_03	usmz_04	Iusmz_04	usmz_05	Iusmz_05	usmz_06	Iusmz_06	usmz_07	Iusmz_07	usmz_08	Iusmz_08	usmz_09	Iusmz_09	usmz_10	Iusmz_10	aiV0	aiV1	aiV2	aiV3	aiV4	aiV5	aiV6	aiV7	'.split('\t')[:-1]
    # 格式
    write_formats = ['04d','03d','8.2f','1d','1d',	'8.2f','08d']+['5.3f']*8+['04d','5.3f']*10 +['5.3f']*8
    read_formats = [format[-1] for format in write_formats]

    # data types
    data_types = dict(zip(col_names, read_formats))

    # 分隔符
    sep = '\t'
    # 默认值，为保持完整性
    defaults_str = '2021	019	31800.59	1	0	33201.70	00000000	1.318	1.529	3.158	3.206	0.408	0.184	0.487	0.059	0030	1.198	0041	0.756	0046	0.890	0048	0.209	0055	0.081	0064	0.275	0069	0.043	0077	0.020	0091	0.027	0202	0.011	0.772	4.411	4.021	1.027	0.967	0.966	0.927	0.866	'

    # 需要上传或生成的列
    selected_cols = 'Sulf,Nitr,Org_Low,Cl'.split(',')

    # 第一行
    header = sep.join(col_names)+sep
    logger.info(header)

    # 生成格式化字符串
    line_write_formatter=sep.join([f'{{{col_name}:{format}}}' for col_name,format in zip(col_names,write_formats)])+sep
    logger.info(line_write_formatter)

    line_read_formatter=sep.join([f'{{{col_name}:{format}}}' for col_name,format in zip(col_names,read_formats)])+sep
    logger.info(line_read_formatter)

    # 简单测试
    parse_result = parse.parse(line_read_formatter,defaults_str)
    logger.info(parse_result)

    # 默认值
    # data_dict_defaults = parse_result.named 

    # example data
    # raw_data_example_filename = r"raw_data\课题3\AMS\AMS_TimeTrace_033270.txt"

    # 建立设备连接
    TBD = TBDeviceMqttClient(host, port, token)
    TBD.connect()
    logger.info('mqtt server connected!')

    for line in tailhead.follow_path(target_log_file,encoding='utf-8'):
        if line is not None:
            logger.debug(f"readline: {line}")
            # 解析
            parse_result = parse.parse(line_read_formatter, line)
            if parse_result is None:
                logger.debug('not a valid data line')
                continue 
            data_line_dict = parse_result.named

            data_dict = {'ts':-1,'values':{}}
            # 时间解析
            # 假设是UTC时间，如果是北京时间需要调整
            time_datetime = datetime.datetime(year=2021,month=1,day=1)+datetime.timedelta(days=int(data_line_dict['DAY']),seconds=float(data_line_dict['SecAftMid']))
            data_dict['ts'] = int(time_datetime.timestamp()*1000) # 转换为ms数

            # 数据抽取
            data_dict['values'] = {col:data_line_dict.get(col,-99999) for col in selected_cols}
            #data_dict['values']['Sulf'] = float(data_line_dict['Sulf'])
            #data_dict['values']['Nitr'] = float(data_line_dict['Nitr'])
            #data_dict['values']['Org_Low'] = float(data_line_dict['Org_Low'])
            #data_dict['values']['Cl'] = float(data_line_dict['Cl'])

            logger.debug(f'data_dict={data_dict}')

            # 上传
            TBD.send_telemetry(data_dict)
        else:
            time.sleep(delay)

if __name__ == "__main__":
    fire.Fire(main)
    # e.g
    # python demo_tail_log_upload.py --DELAY 0.5 --LOG_FILE your_file.log